from utils import status_code


class BaseView(object):
    '''
    以下返回的可以封装成response返回  比如django的  jsonresponse  或者 flask 的jsonify  这里只是个案例
    '''
    def __call__(self, env, start_response):
        method = env['REQUEST_METHOD']
        view_func = getattr(self,method)
        return view_func(env, start_response)



    def GET(self, env, start_response):
        start_response(status_code.METHOD_NOT_ALLOWED, [('Content-Type', 'text/plain')])
        return [b'405 Method not allowed']

    def POST(self, env, start_response):
        start_response(status_code.METHOD_NOT_ALLOWED, [('Content-Type', 'text/plain')])
        return [b'405 Method not allowed']

    def DELETE(self, env, start_response):
        start_response(status_code.METHOD_NOT_ALLOWED, [('Content-Type', 'text/plain')])
        return [b'405 Method not allowed']

    def PUT(self, env, start_response):
        start_response(status_code.METHOD_NOT_ALLOWED, [('Content-Type', 'text/plain')])
        return [b'405 Method not allowed']

    def OPTIONS(self, env, start_response):
        start_response(status_code.HTTP_OK, [
            ('Content-Type', 'text/plain'),
            ('Access-Control-Allow-Origin','*'),
            ('Access-Control-Allow-Headers','Content-Type,Referer'),
            ('Access-Control-Allow-Methods','GET, POST, PUT,DELETE,PATCH'),
        ])
        return [b'405 Method not allowed']

    def PATCH(self, env, start_response):
        start_response(status_code.METHOD_NOT_ALLOWED, [('Content-Type', 'text/plain')])
        return [b'405 Method not allowed']
